package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: UserController
 * @Author: LiuJunMeng
 * @Date: 2020/2/9 18:59
 * @Description: //TODO
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 获取用户菜单权限
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUsername")
    public Result getUsername() throws Exception {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    //马志强代码开始
    /**
     * 分页查询，用于用户页面分页显示数据
     * @param queryPageBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/findByCondition")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean) throws Exception {
        return userService.findByCondition(queryPageBean);
    }

    /**
     * 获取所有的角色role
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAllRole")
    public Result findAllRole() throws Exception {
        try {
            List<Role> roles = userService.findAllRole();
            return new Result(true, "所有角色获取成功",roles);
        } catch (Exception e) {
            return new Result(false, "所有角色获取失败");
        }
    }

    /**
     * 添加用户
     * @return
     * @throws Exception
     */
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) throws Exception {
        try {
            userService.add(user,roleIds);
            return new Result(true, "添加用户成功");
        } catch (Exception e) {
            return new Result(false, "添加用户失败");
        }
    }
    /**
     * 根据ID查询用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User user = userService.findById(id);
            return new Result(true, "此用户数据获取成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "此用户数据获取失败");
        }
    }
    /**
     * 查询外键关系
     * @param id
     * @return
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id) {
        try {
            Integer[] roleIds = userService.findRoleById(id);
            return new Result(true, "用户对应的角色获取成功", roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "用户对应的角色获取失败");
        }
    }
    /**
     * 编辑用户信息
     * @param
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.edit(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改用户数据失败");
        }
        return new Result(true, "修改用户数据成功");
    }
    /**
     * 根据ID删
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        try {
            userService.deleteById(id);
            return new Result(true, "用户删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "用户删除失败");
        }
    }
    //马志强代码over
}
